$OFS = "`r`n"
$scriptname = "winutil.ps1"
# Variable to sync between runspaces
$sync = [Hashtable]::Synchronized(@{})
$sync.PSScriptRoot = $PSScriptRoot
$sync.configs = @{}

if (Test-Path -Path "$($scriptname)")
{
    Remove-Item -Force "$($scriptname)"
}

Write-output '
################################################################################################################
###                                                                                                          ###
### WARNING: This file is automatically generated DO NOT modify this file directly as it will be overwritten ###
###                                                                                                          ###
################################################################################################################
' | Out-File ./$scriptname -Append -Encoding ascii

(Get-Content .\scripts\start.ps1).replace('#{replaceme}',"$(Get-Date -Format yy.MM.dd)") | Out-File ./$scriptname -Append -Encoding ascii

Get-ChildItem .\functions -Recurse -File | ForEach-Object {
    Get-Content $psitem.FullName | Out-File ./$scriptname -Append -Encoding ascii
}

Get-ChildItem .\config | Where-Object {$psitem.extension -eq ".json"} | ForEach-Object {
    $json = (Get-Content $psitem.FullName).replace("'","''")
    $sync.configs.$($psitem.BaseName) = $json | convertfrom-json
    Write-output "`$sync.configs.$($psitem.BaseName) = '$json' `| convertfrom-json" | Out-File ./$scriptname -Append -Encoding ascii
}

$xaml = (Get-Content .\xaml\inputXML.xaml).replace("'","''")

# Dot-source the Get-TabXaml function
. .\functions\private\Get-TabXaml.ps1

## Xaml Manipulation
$tabColumns = Get-TabXaml "applications" 5
$tabColumns | Out-File -FilePath ".\xaml\inputApp.xaml" -Encoding ascii
$tabColumns = Get-TabXaml "tweaks"
$tabColumns | Out-File -FilePath ".\xaml\inputTweaks.xaml" -Encoding ascii
$tabColumns = Get-TabXaml "feature"
$tabColumns | Out-File -FilePath ".\xaml\inputFeatures.xaml" -Encoding ascii

# Assuming inputApp.xaml is in the same directory as main.ps1
$appXamlPath = Join-Path -Path $PSScriptRoot -ChildPath "xaml/inputApp.xaml"
$tweaksXamlPath = Join-Path -Path $PSScriptRoot -ChildPath "xaml/inputTweaks.xaml"
$featuresXamlPath = Join-Path -Path $PSScriptRoot -ChildPath "xaml/inputFeatures.xaml"

# Load the XAML content from inputApp.xaml
$appXamlContent = Get-Content -Path $appXamlPath -Raw
$tweaksXamlContent = Get-Content -Path $tweaksXamlPath -Raw
$featuresXamlContent = Get-Content -Path $featuresXamlPath -Raw

# Replace the placeholder in $inputXML with the content of inputApp.xaml
$xaml = $xaml -replace "{{InstallPanel_applications}}", $appXamlContent
$xaml = $xaml -replace "{{InstallPanel_tweaks}}", $tweaksXamlContent
$xaml = $xaml -replace "{{InstallPanel_features}}", $featuresXamlContent

Write-output "`$inputXML =  '$xaml'" | Out-File ./$scriptname -Append -Encoding ascii

Get-Content .\scripts\main.ps1 | Out-File ./$scriptname -Append -Encoding ascii